﻿//lastIndexOf兼容ＩＥ
Array.prototype.lastIndexOf = function (item) {
    var len = this.length;
    for (var i = len; i >= 0; i--) {
        if (this[i] === item) {
            return len - i;
        }
    }
    return -1;
}

var maxSize = 8 * 1024 * 1024; // byte
var maxCounts = 3;
function SetMaxCounts(counts) {
    maxCounts = counts;
}
function change(file, pic) {
    if (file.files[0].size > maxSize) {
        OR.CL.showMessage("请选择大小在8M以内的文件", false);
        return;
    }

    var index = file.value.lastIndexOf("\\");
    var img_name = file.value.substring(index + 1, file.value.length);
    var originalImg = $(pic).siblings('.original-img').get(0);

    pic.onload = function () {
        var imgWrap = $(file).parents('.thumb-img-box').siblings('.img-wrap');
        $(file).parents('.thumb-img-box').hide();
        $(file).parents('.thumb-item').addClass('hadupload');


        //// 图片宽高
        //var imgw = parseInt($(imgWrap).find('.thumb-img').css('width'));
        //var imgh = parseInt($(imgWrap).find('.thumb-img').css('height'));
        //// 外框宽高
        //var wrapw = parseInt($(imgWrap).width());
        //var wraph = parseInt($(imgWrap).height());
        //// 比例
        //var wrapRadio = wraph / wrapw;
        //var radio = imgh / imgw;
        //// 根据比例计算宽高
        //if (radio > wrapRadio) {
        //    $(imgWrap).find(".thumb-img").css({ "width": wrapw, 'height': 'auto' });
        //} else {
        //    $(imgWrap).find(".thumb-img").css({ "height": wraph, 'width': 'auto' });
        //}
        //// 重新获取宽高
        //imgw = $(imgWrap).find(".thumb-img").width();
        //imgh = $(imgWrap).find(".thumb-img").height();
        // 设置位置
        $(imgWrap).find('.thumb-img').css({
            "position": "absolute"
        });

        $(imgWrap).parents('.thumb-item-wrap').siblings('.info').find('.name').text(img_name);
        $(imgWrap).siblings('.file-radio').find('.check-radio').val(img_name);
        $(imgWrap).siblings('.file-radio').show();

        var count = $('#thumb-wrap .thumb-item').length;
        var usecount = $('#thumb-wrap .thumb-item.hadupload').length;
        if ($('#thumb-wrap .thumb-item').length < maxCounts && (count - usecount) < 1) {
            addItem('#thumb-wrap', img_name);
        }
    }

    var ext = file.value.substring(file.value.lastIndexOf(".") + 1).toLowerCase();

    // gif在IE浏览器暂时无法显示
    if (ext != 'png' && ext != 'jpg') {
        //alert("图片的格式必须为png或者jpg格式！");
        pic.src = 'https://cdn.orayimg.com/console/img/attachment.png';
        $(pic).parent('a').attr('data-attachment', 1);
        return;
    }

    if (window.FileReader) {//chrome,firefox7+,opera,IE10,IE9，IE9也可以用滤镜来实现
        oFReader = new FileReader();
        oFReader.readAsDataURL(file.files[0]);
        oFReader.onload = function (oFREvent) {
            pic.src = oFREvent.target.result;

        };
    }
    else if (document.all) {//IE8-

        file.select();
        file.blur();

        var reallocalpath = document.selection.createRange().text//IE下获取实际的本地文件路径
        if (window.ie6) pic.src = reallocalpath; //IE6浏览器设置img的src为本地路径可以直接显示图片
        else { //非IE6版本的IE由于安全问题直接设置img的src无法显示本地图片，但是可以通过滤镜来实现，IE10浏览器不支持滤镜，需要用FileReader来实现，所以注意判断FileReader先
            pic.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src=\"" + reallocalpath + "\")";
            pic.src = '';//设置img的src为base64编码的透明图片，要不会显示红xx
            originalImg.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='image',src=\"" + reallocalpath + "\")";
            originalImg.src = '';//设置img的src为base64编码的透明图片，要不会显示红xx

        }
    }
    else if (file.files) {//firefox6-
        if (file.files.item(0)) {
            url = file.files.item(0).getAsDataURL();
            pic.src = url;
        }
    }
}


$('#thumb-wrap .thumb-item .link').live('click', function () {
    $(this).parents('.thumb-item').remove();
    var count = $('#thumb-wrap .thumb-item').length;;
    var usecount = $('#thumb-wrap .thumb-item.hadupload').length;
    if ($('#thumb-wrap .thumb-item').length < maxCounts && (count - usecount) < 1) {
        addItem('#thumb-wrap');
    }
});


function addItem(element, img_name) {
    var pp = "change(this,$(this).parents('.thumb-img-box').siblings('.img-wrap').find('img').get(0))";
    var item = $('<div class="thumb-item">' +
        '<div class="thumb-item-wrap">' +
        '<div class="thumb-img-box">' +
        '<input class="file-input" name="file" type="file" onchange=" ' + pp + ' " />' +
        '<i class="iconfont add-icon"> + </i>' +
        '</div>' +
        '<a href="javacript:void(0);" class="img-wrap">' +
        '<img class="thumb-img" src="" >' +
        '<img class="original-img" src="" style="display:none;">' +
        '</a>' +
        '<label class="file-radio" style="display:none" ><input type="radio" class="check-radio" value="" name="first_img"/>设为首图</label>' +
        '</div>' +
        '<div class="clear info">' +
        '<a href="javascript:void(0);" class="link">删除</a>' +
        '<p class="name">' + img_name + '</p>' +
        '</div>' +
        '</div>');
    $(element).append(item);
    var items = $('#thumb-wrap .thumb-item');
    for (var i = 0; i < items.length; i++) {
        $(items[i]).find('.file-input').attr('_name', 'img' + i);
    }
    bindImgBtn();
}
